package com.kaifamiao.month05.day15;

public class ThreadTest {

    public static void main(String[] args) {
        MyRunnable t1 = new MyRunnable(true);
        MyRunnable t2 = new MyRunnable(true);
        MyRunnable t3 = new MyRunnable(false);
        MyRunnable t4 = new MyRunnable(false);

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        System.out.println(MyRunnable.getJ());
    }


}
class MyRunnable extends Thread {
    private static int j;

    public static int getJ() {
        return j;
    }

    private final boolean flag;

    public MyRunnable(boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        synchronized(this) {
            if (flag) {
                System.out.println(j);
                for (int i = 0;i < 100; i++,j++);
            } else {
                System.out.println(j);
                for (int i = 0;i < 100; i++,j--);
            }
        }
    }
}
